<?php

namespace App\Http\Validate\Kms\Attendance;

use App\Http\Validate\AbstractBaseValidate;
use App\Models\Kms\Album\AlbumResourceModel;
use App\Models\Kms\Attendance\AttendanceDailyGatherModel;
use App\Models\Kms\Attendance\AttendanceDailyPointTraceModel;
use App\Models\Kms\User\KmsUser;
use App\Models\Kms\User\KmsUserConfigModel;
use App\Service\Kms\Album\AlbumMainService;
use App\Service\Kms\System\SystemService;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class AttendanceValidate extends AbstractBaseValidate
{
    function getRules(): array
    {
        // TODO: Implement getRules() method.
        return [];
    }

    function getAttributes(): array
    {
        // TODO: Implement getAttributes() method.
        return [];
    }

    function registerValidateRules(): void
    {
        // TODO: Implement registerValidateRules() method.
        // TODO: Implement registerValidateRules() method.
        Validator::extend('mobile', function ($attribute, $value, $parameters, $validator) {
            return $validator->validateRegex(
                $attribute,
                $value,
                ['/^1\d{10}$/'],
                "手机号格式非法"
            );
        });
    }

    function getMessages(): array
    {
        // TODO: Implement getMessages() method.
        return [];
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function dailyPointTraceUploadValidate(array $params)
    {
        return self::getInstance()->validate(
            ["tracePoints"],
            $params,
            [
                "tracePoints"                         => "required|array|min:1|max:10000",
                "tracePoints.*.tracePointTime"        => "required|date_format:Y-m-d H:i:s",
                "tracePoints.*.gpsSwitch"             => self::intInValidateBuild(true,
                    AttendanceDailyPointTraceModel::GPS_SWITCH_TEXT),
                "tracePoints.*.wifiSwitch"            => self::intInValidateBuild(true,
                    AttendanceDailyPointTraceModel::WIFI_SWITCH_TEXT),
                "tracePoints.*.cellularNetworkSwitch" => self::intInValidateBuild(true,
                    AttendanceDailyPointTraceModel::CELLULAR_NETWORK_SWITCH_TEXT),
                "tracePoints.*.dumpEnergy"            => "required|int|min:0|max:100",
                "tracePoints.*.longitude"             => [
                    "string",
                    "regex:/^[\-\+]?(0?\d{1,2}(\.\d{1,6})*|1[0-7]?\d{1}(\.\d{1,6})*|180(\.0{1,6})*)$/"
                ],
                "tracePoints.*.latitude"              => [
                    "string",
                    "regex:/^[\-\+]?([0-8]?\d{1}(\.\d{1,6})*|90(\.0{1,6})*)$/"
                ],
                "tracePoints.*.deviceModel"           => self::stringValidateBuild(true, 64, 1),
                "tracePoints.*.deviceSerialNumber"    => self::stringValidateBuild(true, 120, 1),
                "tracePoints.*.deviceSystem"          => self::stringValidateBuild(true, 120, 1),
                "tracePoints.*.extra"                 => "array",
            ]
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function fieldTraceDetailListValidate(array $params)
    {
        return self::getInstance()->validate(
            ["id"],
            $params,
            [
                "id" => [
                    "required",
                    "int",
                    Rule::exists((new AttendanceDailyGatherModel())->getTable(), "id")
                ]
            ]
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function fieldTraceListValidate(array $params)
    {
        return self::getInstance()->validate(
            ["uid", "date"],
            $params,
            [
                "uid"  => [
                    "required",
                    "int"
                ],
                "date" => "required|string|date_format:Y-m-d"
            ]
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function fieldSwitchValidate(array $params)
    {
//        $departmentId = $request->get("departmentId", "");
//        $jobTitle     = $request->get("jobTitle", "");
//        $name         = $request->get("name", "");
//        $fieldSwitch  = $request->get("fieldSwitch", null);
//        $uid          = $request->get("uid", []);
        return self::getInstance()->validate(
            ["status", "jobTitle", "departmentId", "name", "fieldSwitch", "uid"],
            $params,
            [
                "jobTitle"     => "string",
                "departmentId" => "string",
                "name"         => "string",
                "fieldSwitch"  => "int|in:0,1",
                "uid"          => [
                    "array"
                ],
                "uid.*"        => [
                    "int",
                    "min:0"
                ],
                "status"       => self::intInValidateBuild(true, KmsUserConfigModel::FIELD_SWITCH_TEXT)
            ]
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function fieldListValidate(array $params)
    {
        return self::getInstance()->validate(
            ["pageSize", "pageNum", "uid", "startDate", "endDate", "status"],
            $params,
            [
                "pageSize"  => "int|min:1",
                "pageNum"   => "int|min:1",
                "uid"       => "int|min:1",
                "startDate" => "date_format:Y-m-d",
                "endDate"   => "date_format:Y-m-d",
                "status"    => self::intInValidateBuild(false, AttendanceDailyGatherModel::STATUS_TEXT)
            ]
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function fieldGroupListValidate(array $params)
    {
        return self::getInstance()->validate(
            [
                "pageSize",
                "pageNum",
                "uid",
                "startDate",
                "endDate",
                "jobTitle",
                "departmentId",
                "mobile",
                "sortField",
                "sortOrder"
            ],
            $params,
            [
                "pageSize"     => "int|min:1",
                "pageNum"      => "int|min:1",
                "uid"          => "int|min:1",
                "startDate"    => "date_format:Y-m-d",
                "endDate"      => "date_format:Y-m-d",
                "jobTitle"     => "string",
                "departmentId" => "string",
                "mobile"       => "string|mobile",
                "sortField"    => "string|in:abnormalDays,missDays,normalDays",
                "sortOrder"    => [
                    Rule::requiredIf((bool)($params["sortField"] ?? "")),
                    "string",
                    Rule::in(["ascend", "descend"])
                ],
            ]
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function punchShareValidate(array $params)
    {
        $groupIdsStr = collect(AlbumMainService::groupList(false)["groupList"])->implode("id", ",");
        return self::getInstance()->validate(
            ["punchType", "remark", "resourceId", "groupIds"],
            $params,
            [
                "punchType"   => ["required", "array"],
                "punchType.*" => self::intInValidateBuild(true, SystemService::PUNCH_TYPE_TEXT),
                "remark"      => self::stringValidateBuild(false, 200),
                "resourceId"  => [
                    "required",
                    "int",
                    Rule::exists((new AlbumResourceModel())->getTable(), "id")
                ],
                "groupIds"    => [
                    "required",
                    "array"
                ],
                "groupIds.*"  => [
                    "int",
                    "in:" . $groupIdsStr
                ],
            ]
        );
    }
}
